<template>
	<view class="addCoupon">
		<uni-forms :modelValue="formData">
			<uni-forms-item label="优惠劵名称" name="name" :label-width="120">
				<uni-easyinput type="text" v-model="formData.name" placeholder="请输入优惠劵名称" />
			</uni-forms-item>
			<uni-forms-item label="金额" name="price" :label-width="120">
				<view style="display: flex; align-items: center">
					<text style="margin-right: 10rpx">￥</text>
					<u-number-box step="0.1" decimal-length="2" v-model="formData.price" :min="0.01" inputWidth="100"></u-number-box>
				</view>
			</uni-forms-item>
			<uni-forms-item required name="full" label="满减金额" :label-width="120">
				<view style="display: flex; align-items: center">
					<text style="margin-right: 10rpx">￥</text>
					<u-number-box step="0.1" decimal-length="2" v-model="formData.full" :min="0.01" inputWidth="100"></u-number-box>
				</view>
			</uni-forms-item>
			<uni-forms-item required name="is_auto" label="是否自动领取" :label-width="120">
				<view style="display: flex; align-items: center">
					<u-switch activeColor="#2979ff" v-model="formData.is_auto" activeValue="1" inactiveValue="0"></u-switch>
				</view>
			</uni-forms-item>
		</uni-forms>
		<button @click="getstoreCouponSava" type="default" style="background-color: #2979ff; color: #fff">
			{{isEdit?'保存编辑':'新增优惠券'}}
		</button>
	</view>
</template>

<script>
import { storeCouponSava } from '../../../api/payment/index.js';
export default {
	data() {
		return {
			formData: {
				name: '',
				price: 1,
				full: 1,
				is_auto: '0',
				type: ''
			},
			isEdit:false
		};
	},
	onLoad(info) {
		const { name, price, full ,id,is_auto} = info;
		this.formData.name = name ? decodeURI(name) : '';
		this.formData.price = price || 1;
		this.formData.full = full || 1;
		this.formData.is_auto = is_auto || '0'
		if(id){
			this.isEdit = true;
			this.formData.id = id;
			uni.setNavigationBarTitle({
				title: '编辑优惠卷'
			});
		}
	},
	methods: {
		async getstoreCouponSava() {
			if (!this.formData.name) {
				return uni.showToast({
					title: '优惠券名称不能为空',
					icon: 'none'
				});
			}
			this.formData.type = this.$store.state.storeType;
			try {
				const resData = await storeCouponSava(this.formData);
				if (resData.code == 1) {
					uni.showToast({
						title: '操作成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.navigateBack(-1);
					}, 200);
				}
			} catch (e) {
				throw Error(e);
			}
		}
	}
};
</script>

<style>
page {
	background-color: #fff;
}
</style>

<style scoped lang="scss">
.addCoupon {
	box-sizing: border-box;
	padding: 20rpx;
}
</style>
